﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmThongKeDen : Form
    {
        public frmThongKeDen()
        {
            InitializeComponent();
        }
        int _nam;

        private void frmThongKeDen_Load(object sender, EventArgs e)
        {
            rdbThang.Checked = true;
            txtNam.Text = DateTime.Today.Year.ToString();            
            btnLuu.Enabled = false;
        }

        private void txtNam_TextChanged(object sender, EventArgs e)
        {
            bool result = true;
            int tmp=0;
            if (!int.TryParse(txtNam.Text.Trim(), out tmp))
            {
                result = false;
            }
            if (tmp <= 1975 || tmp>=9999)
            {
                result = false;
            }

            btnThongKe.Enabled = result;
            _nam = tmp;
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            try
            {
                dgvKetQua.DataSource = CongVanDenBUS.ThongKe(_nam, rdbThang.Checked);
                if (dgvKetQua.RowCount > 0)
                {
                    btnLuu.Enabled = true;
                }
                else
                {
                    btnLuu.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                String pathname = "";
                SaveFileDialog savediaglog = new SaveFileDialog();
                savediaglog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
                savediaglog.Filter = "Excel (*.xls)|*.xls|All Files (*.*)|*.*";
                savediaglog.FilterIndex = 1;

                if (savediaglog.ShowDialog() == DialogResult.OK)
                {
                    pathname = savediaglog.FileName;


                    if (pathname != "")
                    {

                        DataTable data = (DataTable)dgvKetQua.DataSource;
                        if (data != null)
                        {
                            for (int i = 0; i < data.Columns.Count; i++)
                            {
                                data.Columns[i].Caption = dgvKetQua.Columns[i].HeaderText;
                            }
                            CongVanDenBUS.Luu(pathname, data);
                        }
                        else
                            MessageBox.Show("Không Có Dữ Liệu ");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
